interface TableApi {
  getList: (row?: any) => Promise<any>;
  deleteList: (row?: any) => Promise<any>;
  addList: (row?: any) => Promise<any>;
  updateList: (row?: any) => Promise<any>;
}
import { ElMessageBox, ElMessage } from "element-plus";
export function useAvueTableMixin(
  TableApi: TableApi,
  options: any = { customLoadData: null }
) {
  const tableForm = ref<any>({});
  const page = ref({
    currentPage: 1,
    pageSize: 10,
    total: 0,
  });
  const tableLoading = ref(false);
  const tableData = ref<any>([]);
  const params = ref({});
  const staticParams = ref({});
  /**
   * 获取列表数据
   * @param tempPage 临时页码，用于切换页码时，不刷新列表数据
   */
  const loadData =
    options.customLoadData ||
    ((tempPage?: number) => {
      if (tempPage) {
        page.value.currentPage = tempPage;
      }
      tableLoading.value = true;
      TableApi.getList({
        pageno: page.value.currentPage,
        pagesize: page.value.pageSize,
        ...params.value,
        ...staticParams.value,
      })
        .then((res: any) => {
          if (Array.isArray(res)) {
            tableData.value = res as any;
            return;
          }
          tableData.value = res.data;
          console.log("asdfasdf", res);
          page.value.total = res.total;
        })
        .finally(() => {
          tableLoading.value = false;
        });
    });
  /**
   * 保存数据
   */
  const rowSave = (row: any, done: any, loading: any) => {
    const tempParams = Object.fromEntries(
      Object.entries({ ...row, ...staticParams.value }).filter(
        ([key, value]) => value !== ""
      )
    );
    TableApi.addList(JSON.parse(JSON.stringify(tempParams)))
      .then((res) => {
        done();
        loadData();
      })
      .catch((err) => {
        loading();
      });
  };
  /**
   * 更新数据
   */
  const rowUpdate = (row: any, index: any, done: any, loading: any) => {
    TableApi.updateList(JSON.parse(JSON.stringify(row)))
      .then((res) => {
        done();
        loadData();
      })
      .finally(() => {
        loading();
      });
  };
  const rowDel = (row: any, index: any, done: any) => {
    ElMessageBox({
      title: "提示",
      message: "确定删除吗？",
      type: "warning",
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      beforeClose: (action, instance, done) => {
        if (action === "confirm") {
          instance.confirmButtonLoading = true;
          TableApi.deleteList(row.id)
            .then(() => {
              done();
              loadData();
            })
            .finally(() => {
              instance.confirmButtonLoading = false;
            });
        } else {
          done();
        }
      },
    }).then(() => {
      ElMessage({
        type: "success",
        message: "删除成功",
      });
    });
  };
  const searchChange = (tparams: any, done: any) => {
    if (done) done();
    params.value = tparams;
    loadData(1);
  };
  return {
    tableForm,
    page,
    tableLoading,
    tableData,
    params,
    loadData,
    rowSave,
    rowUpdate,
    rowDel,
    searchChange,
    staticParams,
  };
}
